<?php

//PLUGIN type=backend
//PLUGIN name=Sitemap backend
//PLUGIN desc=Adds a shortcut in the backend for the sitemap plugin
//PLUGIN group=general

global $options; // We read from $options
global $toolbar; // Adds to the toolbar global
$siteurl = __SITE_URL__;

function createSitemap($in, $a = array())
{
    if (!empty($in['url']))
    {
        $a[] = $in['url'];
    }
    if (is_array($in["children"]))
    {
        foreach ($in["children"] as $c)
        {
            createSitemap($c, &$a);
        }
    }
    return $a;
}

if ($options['viewsitemap'])
{
    header("Content-Type: text/plain charset=UTF-8");
    print "THIS IS NOT THE REAL SITEMAP, JUST A PREVIEW!\n";
    print "real sitemap url: {$_SERVER['SERVER_NAME']}{$siteurl}/sitemap.xml\n";
    print "\n-----------------------------------------------------------\n\n";
    print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
    print "<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\"
            xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
            xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">\n";
    foreach (createSitemap(menu(_CONTENTPATH_)) as $x)
    {
        print "\t<url>\n";
        print "\t\t<loc>{$x}</loc>\n";
        print "\t</url>\n";
    }
    print '</urlset>';
    exit();
}



$toolbar->add(
        'SITEMAP',
        '?q=viewsitemap;',
        'style/icons/Smiley_Star.png',
        "View the page sitemap.\nThe sitemap can be found at {$_SERVER['SERVER_NAME']}{$siteurl}/sitemap.xml"
);
?>